package packVistas;

import javax.swing.JDialog;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import packLigaDeFutbol.HistoricoTemporada;
import packLigaDeFutbol.UtilControles;
import packLigaDeFutbol.comboValue;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SeleccionTemporada extends JDialog {

	private static final long serialVersionUID = 2311664961642920049L;
	private boolean cargando;

	final JComboBox<comboValue> cboJornadaTemporada = new JComboBox<comboValue>();
	final JComboBox<comboValue> cboAnioTemporada  = new JComboBox<comboValue>();
	
	public SeleccionTemporada(final JDialog pDestino) {
		cargando = true;
		setResizable(false);
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Elegir Temporada");
		setBounds(100, 100, 234, 179);
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("center:default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,}));

		JLabel lblNewLabel = new JLabel("Seleccione la temporada deseada");
		getContentPane().add(lblNewLabel, "4, 2");

		cboAnioTemporada.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (!cargando){
					
					int idTemporada;
					JComboBox<comboValue> cb = (JComboBox<comboValue>)e.getSource();
					
					idTemporada = ((comboValue)cb.getSelectedItem()).getValor();
					
					cboJornadaTemporada.removeAllItems();
					
					UtilControles.cargarCombo(cboJornadaTemporada, HistoricoTemporada.get().obtenerJornadasDeTemporada(idTemporada));
				}
			}
		});
		getContentPane().add(cboAnioTemporada, "4, 4, fill, default");

		getContentPane().add(cboJornadaTemporada, "4, 8, fill, default");

		JLabel lblNewLabel_2 = new JLabel("Seleccione la jornada deseada");
		getContentPane().add(lblNewLabel_2, "4, 6");

		JPanel panel = new JPanel();
		getContentPane().add(panel, "4, 10");
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("center:default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("center:default:grow"),},
				new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,}));

		JButton btnAcetpar = new JButton("Aceptar");
		btnAcetpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnAcetpar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (pDestino instanceof ConsultarPartido){
					int idJornada = ((comboValue) cboJornadaTemporada.getSelectedItem()).getValor();
					((ConsultarPartido) pDestino).cargarDatos(idJornada);
				}
				else if(pDestino instanceof Estadisticas) {
					int idJornada = ((comboValue) cboJornadaTemporada.getSelectedItem()).getValor();
					int idTemp = ((comboValue) cboAnioTemporada.getSelectedItem()).getValor();
					((Estadisticas) pDestino).cargarDatos(idTemp,idJornada);
				}

				pDestino.setVisible(true);
			}
		});
		panel.add(btnAcetpar, "1, 1");

		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
		});
		panel.add(btnCancelar, "3, 1");

		cargando = true;
		UtilControles.cargarCombo(cboAnioTemporada, HistoricoTemporada.get().obtenerFechasDeTodasTemporadas());
		cargando = false;

		if (cboAnioTemporada.isEnabled()){
			setVisible(true);
		}else{
			JOptionPane.showMessageDialog(new JFrame(), 
					"No existen Temporadas en la base de datos.", 
					getTitle(),JOptionPane.INFORMATION_MESSAGE);
			dispose();
		}
	}

}